<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\system;

use think\facade\Db;
use app\traits\CategoryRepository;
use app\common\dao\system\SystemRegionDao;
use app\common\repositories\BaseRepository;

/**
 * Class SystemRegionRepository
 * @mixin SystemRegionDao
 */
class SystemRegionRepository extends BaseRepository
{
    use CategoryRepository;

    const REGION_LEVE = [
        1 => '省',
        2 => '市',
        3 => '区/县',
    ];

    public function __construct(SystemRegionDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 地区列表
     */
    public function getList($page, $limit, $level, $pid)
    {
        $where = ['level' => $level, 'pid' => $pid];
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->field('id,name,level,merger_name,area_code,remark')
            ->select();
        foreach ($list as $k => $v) {
            $list[$k]['level'] = self::REGION_LEVE[$v['level']] ?? '';
        }
        return compact('count', 'list');
    }


    public function getChildren(array $where)
    {
        return $this->search($where)
            ->field('id,name,level,merger_name')
            ->select();
    }


    public function addInfo(array $data, int $userId = 0)
    {
        if ($data['pid'] > 0) {
            $data['merger_name'] = $this->dao->getSearch([])
                    ->where('id', $data['pid'])->value('merger_name') . ' ' . $data['name'];
        } else {
            $data['merger_name'] = $data['name'];
        }
        return $this->create($data);
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        if ($data['pid'] > 0) {
            $data['merger_name'] = $this->dao->getSearch([])
                    ->where('id', $data['pid'])->value('merger_name') . ' ' . $data['name'];
        } else {
            $data['merger_name'] = $data['name'];
        }
        return $this->dao->update($info['id'], $data);
    }


    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    /**
     * 删除单条数据
     * @param $info
     * @param int $userId
     * @return int
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-01-18 22:02
     */
    public function deleteInfo($info, int $userId = 0)
    {
        return $this->dao->delete($info['id']);
    }
}